package com.ctshk.rpc.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.common.utils.SnowflakeIdWorker;
import com.ctshk.rpc.system.dto.LocalFunIncoDTO;
import com.ctshk.rpc.system.dto.MainDataFunTypeDTO;
import com.ctshk.rpc.system.entity.LocalFunIcon;
import com.ctshk.rpc.system.mapper.MainDataLocalFunMapper;
import com.ctshk.rpc.system.req.MainDataLocalFunAddReq;
import com.ctshk.rpc.system.service.IMainDataLocalFunService;
import com.github.xiaoymin.knife4j.core.util.CollectionUtils;
import com.google.common.collect.Maps;
import org.apache.dubbo.config.annotation.DubboService;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description
 * @Author wansi <gaylen15527491065@yeah.net>
 * @Date 2021/4/22
 */
@DubboService
public class MainDataLocalFunServiceImpl extends ServiceImpl<MainDataLocalFunMapper, LocalFunIcon> implements IMainDataLocalFunService {

    @Override
    public Result<PageResponse<LocalFunIncoDTO>> list(MainDataLocalFunAddReq req, TokenUser currentUser) {
        IPage<LocalFunIcon> iPage = new Page<>(req.getPageNo(), req.getPageSize());
        QueryWrapper<LocalFunIcon> queryWrapper = new QueryWrapper<>();
        if(req.getStatus() != null){
            queryWrapper.lambda().eq(LocalFunIcon::getStatus,req.getStatus());
        }
        if(req.getId() != null){
            queryWrapper.lambda().eq(LocalFunIcon::getId,req.getId());
        }
        IPage<LocalFunIcon> localFunIconIPage = baseMapper.selectPage(iPage, queryWrapper);
        List<LocalFunIncoDTO> localFunIncoDTOS = EntityUtil.copyList(localFunIconIPage.getRecords(), LocalFunIncoDTO.class);
        return Result.success(new PageResponse<>(localFunIncoDTOS, localFunIconIPage.getCurrent() == 1, localFunIconIPage.getTotal() <= (localFunIconIPage.getCurrent() * localFunIconIPage.getSize()), localFunIconIPage.getTotal(), localFunIconIPage.getCurrent(), localFunIconIPage.getSize()));
    }

    @Override
    public Result add(MainDataLocalFunAddReq req, TokenUser currentUser) {
        LocalFunIcon localFunIcon = EntityUtil.copy(req, LocalFunIcon.class);
        localFunIcon.setId(SnowflakeIdWorker.nextId());
        localFunIcon.setGmtCreate(LocalDateTime.now());
        localFunIcon.setStatus(0);
        localFunIcon.setModifiedId(currentUser.getId());
        localFunIcon.setGmtModified(LocalDateTime.now());
        return Result.success(baseMapper.insert(localFunIcon));
    }

    @Override
    public Result edit(MainDataLocalFunAddReq req, TokenUser currentUser) {
        LocalFunIcon localFunIcon = baseMapper.selectById(req.getId());
        localFunIcon.setName(req.getName());
        localFunIcon.setPhotos(req.getPhotos());
        localFunIcon.setModifiedId(currentUser.getId());
        localFunIcon.setGmtModified(LocalDateTime.now());
        return Result.success(baseMapper.updateById(localFunIcon));
    }

    @Override
    public Result getFunType() {
        QueryWrapper<LocalFunIcon> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(LocalFunIcon::getStatus,0);
        List<LocalFunIcon> localFunIcons = baseMapper.selectList(queryWrapper);
        if(CollectionUtils.isNotEmpty(localFunIcons)){
            return Result.success(localFunIcons.stream().map(entity->{
                MainDataFunTypeDTO dto = new MainDataFunTypeDTO();
                dto.setId(entity.getId());
                dto.setName(entity.getName());
                return dto;
            }).collect(Collectors.toList()));
        }
        return Result.success();
    }

    @Override
    public Result updateStatus(MainDataLocalFunAddReq mainDataLocalFunAddReq) {
        LocalFunIcon localFunIcon = baseMapper.selectById(mainDataLocalFunAddReq.getId());
        localFunIcon.setStatus(mainDataLocalFunAddReq.getStatus());
        baseMapper.updateById(localFunIcon);
        return Result.success();
    }

    @Override
    public Result<PageResponse<LocalFunIncoDTO>> getTypeList(MainDataLocalFunAddReq req, TokenUser currentUser) {
        IPage<LocalFunIcon> iPage = new Page<>(req.getPageNo(), req.getPageSize());
        LambdaQueryWrapper<LocalFunIcon> eq = new QueryWrapper<LocalFunIcon>().lambda().eq(LocalFunIcon::getStatus, 0);
        IPage<LocalFunIcon> localFunIconIPage = baseMapper.selectPage(iPage, eq);
        List<LocalFunIncoDTO> localFunIncoDTOS = EntityUtil.copyList(localFunIconIPage.getRecords(), LocalFunIncoDTO.class);
        return Result.success(new PageResponse<>(localFunIncoDTOS, localFunIconIPage.getCurrent() == 1, localFunIconIPage.getTotal() <= (localFunIconIPage.getCurrent() * localFunIconIPage.getSize()), localFunIconIPage.getTotal(), localFunIconIPage.getCurrent(), localFunIconIPage.getSize()));
    }

    @Override
    public Map<Long, com.ctshk.rpc.system.dto.LocalFunIcon> getFunSortMapping() {
        Map<Long, com.ctshk.rpc.system.dto.LocalFunIcon> map = Maps.newHashMap();
        QueryWrapper<LocalFunIcon> eq = new QueryWrapper<>();
        List<LocalFunIcon> localFunIcons = baseMapper.selectList(eq);
        Map<Long, List<LocalFunIcon>> lunMap = localFunIcons.stream().collect(Collectors.groupingBy(LocalFunIcon::getId));
        lunMap.forEach((k,v)->{
            LocalFunIcon localFunIcon = v.stream().findFirst().orElse(null);
            if(localFunIcon != null){
                com.ctshk.rpc.system.dto.LocalFunIcon entity = EntityUtil.copy(localFunIcon, com.ctshk.rpc.system.dto.LocalFunIcon.class);
                map.put(k,entity);
            }
        });
        return map;
    }
}
